import { User } from "../../../modeal/user.js"
const user = new User()
Page({
  data: {
    shopInfo: {},
    carInfo: {},
    deskid: '',
    eatInOid: '',
    dinnerNum: 1,
    remarkText: '口味,偏好等',
    isAdd: false,  //是否属于加菜
  },
  onLoad: function () {
    let shopInfo = wx.getStorageSync('shopInfo')
    let deskid = wx.getStorageSync('deskid')
    let eatInOid = wx.getStorageSync('eatInOid')
    this.setData({ shopInfo, deskid, eatInOid })
    this.getCarList()  // 获取购物车
  },

  onShow: function () {
    // 检测当前桌号是否有就餐
    this.getDiningInfo()
  },


  // 获取购物车
  getCarList () {
    let shopId = this.data.shopInfo.id
    let orderType = '1'
    user.getCarList(shopId, orderType).then(res => {
      this.setData({ carInfo: res })
    })
  },

  // 购物车减少
  cut (e) {
    let item = e.currentTarget.dataset.item
    this.carAddOrCut(item)
  },

  // 购物车增加
  add (e) {
    let item = e.currentTarget.dataset.item
    this.carAddOrCut(item, 'add')
  },


  // 购物车添加和减少
  carAddOrCut (item, way) {
    let shopId = this.data.shopInfo.id
    let gid = item.gid
    let orderType = '1'
    let attr_value = ""
    let attr_id = 0
    item.sku_id ? (attr_value = item.sku_value, attr_id = item.sku_id) : ''
    if (way == "add") {
      user.addCar(gid, shopId, orderType, attr_id, attr_value).then(res => {
        this.setData({ carInfo: res })
      })
    } else {
      user.cutCar(gid, shopId, orderType, attr_id, attr_value).then(res => {
        this.setData({ carInfo: res })
      })
    }
  },

  // 去下单
  // 这里需要判断是否属于加菜  ->判断当前餐桌是否有就餐
  goPlace () {
    let isAdd = this.data.isAdd;
    isAdd ? this.addDish() : this.placeOrder()
  },

  // 下单
  //  address_id,coupon_id,date,deskid,jc_count,location, ordertype,paytype,remark,shops,time,lastpay,mobile} 
  placeOrder () {
    let address_id = 0
    let coupon_id = 0
    let date = ""
    let deskid = this.data.deskid
    let jc_count = 0
    let location = {}
    let orderType = "1"
    let paytype = 1
    let remark = ''
    let shops = this.data.shopInfo.shopname
    let time = ''
    let lastpay = 1
    let mobile = ''
    user.placeOrder(address_id, coupon_id, date, deskid, jc_count, location, orderType, paytype, remark, shops, time, lastpay, mobile).then(res => {
      wx.setStorageSync('eatInOid', res)
      wx.redirectTo({ url: "/pages/shopping/eatInOrder/eatInOrder" })
    })
  },

  // 判断是否属于加菜
  getDiningInfo () {
    let deskid = this.data.deskid
    user.getDiningInfo(deskid).then(res => {
      wx.setStorageSync('eatInOid', res.oid)
      this.setData({ isAdd: true })
    })
  },

  // 加菜
  addDish () {
    let eatInOid = wx.getStorageSync('eatInOid')
    let shopId = this.data.shopInfo.id
    wx.showModal({
      content: "您确定要加菜吗?",
      success: res => {
        if (res.confirm) {
          user.addDish(eatInOid, shopId).then(res => {
            wx.redirectTo({ url: "/pages/shopping/eatInOrder/eatInOrder" })
          })
        }
      }
    })

  },


  // 继续点菜
  shoppingGoOn () {
    wx.redirectTo({ url: "/pages/shopping/index/index" })

  }

})